<?php
namespace app\admin\model;

use think\Model;
// use think\Db;
class DemandInfoModel extends Model
{
    protected $table = 'vd_demand_info';


    public function get_data_info($name=null,$id){
        $data = $this->where('id',$id)->find();
        return $data;
    }

    //待审核数据
    public function get_audit_data($name=null){
        $data = $this->where('audit_status',2)->order('id desc')->paginate(15);
        $count = $this->where('audit_status',2)->order('id desc')->count();
        $page = $data->render();
        return ['data'=>$data,'page'=>$page,'count'=>$count];
    }


    //获取所有数据
    public function get_all_data($name=null){
        $data = $this->alias('r')
                     ->where('ur.release_status',1)
                     // ->where('r.audit_status',1)
                     ->join('vd_user_demand ur','r.id=ur.demand_info_id')
                     ->order('r.id desc')
                     ->field('r.*,ur.open_status,ur.id urid')
                     ->paginate(15);
        $count = $this->alias('r')
                     ->where('ur.release_status',1)
                     // ->where('r.audit_status',1)
                     ->join('vd_user_demand ur','r.id=ur.demand_info_id')
                     ->order('r.id desc')
                     ->field('r.*,ur.open_status,ur.id urid')
                     ->count();
        $page = $data->render();
        return ['data'=>$data,'page'=>$page,'count'=>$count];
    }

    //获取首页展示数据
    public function get_show_data($name=null){
        $data = $this->alias('r')
                     ->where('ur.release_status',1)
                     ->where('r.audit_status',1)
                     ->where('r.show_status',1)
                     ->join('vd_user_demand ur','r.id=ur.demand_info_id')
                     ->order('field desc')
                     ->field('r.*,ur.open_status,ur.id urid')
                     ->paginate(15);
        $count = $this->alias('r')
                     ->where('ur.release_status',1)
                     ->where('r.audit_status',1)
                     ->where('r.show_status',1)
                     ->join('vd_user_demand ur','r.id=ur.demand_info_id')
                     ->order('field desc')
                     ->field('r.*,ur.open_status,ur.id urid')
                     ->count();
        $page = $data->render();
        return ['data'=>$data,'page'=>$page,'count'=>$count];
    }


    public function get_data($name=null,$type,$table_name){
        $data = $this->alias('u')
                     ->where(['u.status'=>1,'u.type'=>$type])
                     ->join( $table_name.' e','e.uid=u.id')
                     ->order('u.id desc')
                     ->field('e.*')
                     ->paginate(15);
        $page = $data->render();
        return ['data'=>$data,'page'=>$page];
    }


    public function do_add($name=null,$param){
        $num = $this->save($param);
        return $num;
    }

    public function do_edit($name=null,$param){
        $num = $this->isUpdate(true)->save($param);
        return $num;
    }
    //修改前台展示状态
    public function do_edit_show_status($name=null,$id,$show_status){
      $this->where('id',$id)->update(['show_status'=>$show_status]);
      return $this->where('id',$id)->value('show_status');
    }

    public function do_delete($name=null,$id){
        $num = $this->destroy($id);
        return $num;
    }

    //审核
    public function do_audit($name=null,$id,$audit_status=1){
        $num = $this->where('id',$id)->update(['audit_status'=>$audit_status]);
        return $num;
    }

    //获取uid type
    public function get_email($name=null,$id,$user_table,$info_id){
        $data = $this->alias('r')
                      ->where(['r.id'=>$id,'t.release_status'=>1])
                      ->join($user_table.' t','t.'.$info_id.'=r.id')
                      ->field('t.uid')
                      ->find();
        return $data;
    }

    //获取需求或研究成果
    public function get_r_d($name=null,$id,$table_name,$table_name_info,$info_id,$where='release_status'){
        $data = $this->alias('u')
                     ->where(['u.id'=>$id,$where=>1])
                     ->join($table_name.' r','u.id=r.uid')
                     ->join($table_name_info.' i','i.id=r.'.$info_id)
                     ->field('i.*,r.*')
                     ->paginate(15);
        $page = $data->render();
        return ['data'=>$data,'page'=>$page];
    }


    //搜索
    public function search_data($name=null,$where){
        $data = $this->alias('r')
                     ->where('name','like',$where)
                     ->where('ur.release_status',1)
                     ->where('r.audit_status',2)
                     ->order('r.id desc')
                     ->join('vd_user_demand ur','r.id=ur.demand_info_id')
                     ->order('r.id desc')
                     ->field('r.*,ur.open_status,ur.id urid')
                     ->paginate(15);
        $count = $this->alias('r')
                     ->where('name','like',$where)
                     ->where('r.audit_status',2)
                     ->where('ur.release_status',1)
                     ->order('r.id desc')
                     ->join('vd_user_demand ur','r.id=ur.demand_info_id')
                     ->order('r.id desc')
                     ->field('r.*,ur.open_status,ur.id urid')
                     ->count();
        $page = $data->render();
        return ['data'=>$data,'page'=>$page,'count'=>$count];
    }
    //搜索
    public function search_data_($name=null,$where){
        $data = $this->alias('r')
                     ->where('name','like',$where)
                     ->where('ur.release_status',1)
                     // ->where('r.audit_status',2)
                     ->order('r.id desc')
                     ->join('vd_user_demand ur','r.id=ur.demand_info_id')
                     ->order('r.id desc')
                     ->field('r.*,ur.open_status,ur.id urid')
                     ->paginate(15);
        $count = $this->alias('r')
                     ->where('name','like',$where)
                     // ->where('r.audit_status',2)
                     ->where('ur.release_status',1)
                     ->order('r.id desc')
                     ->join('vd_user_demand ur','r.id=ur.demand_info_id')
                     ->order('r.id desc')
                     ->field('r.*,ur.open_status,ur.id urid')
                     ->count();
        $page = $data->render();
        return ['data'=>$data,'page'=>$page,'count'=>$count];
    }









}